limsSample.abnormalConfirmPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-sample-abnormal-stop-list-grid',
            url: limsSample.batchSampleUrl + '/select/samples',
            requestData: {
                status_EQ : 'abnormalConfirm',
                abnormalHandleType_EQ : 'stop',
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            columns: limsSample.getSampleAbnormalListColumns(),
            page: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.refreshTabInfo();
                }
            },
            onRowActive: function (index, row) {
                _this.refreshTabInfo(row);
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-abnormal-stop-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                        let url = limsSample.baseUrl + '/abnormal/confirm'
                        Gikam.postText(url, Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                            grid.refresh()
                        })
                    })
                }
            },{
                type: 'form',
                id: 'warp-form',
                fields: [{
                    field: 'sampleCode_CISC',
                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                }],
            }],
            onCellClick: function (field, row) {
                if (field === 'ext$.batchcode' && row.ext$.batchid) {
                    Gikam.loadBatchDetailPageV12(row.ext$.batchid, true)
                } else if (field === 'sampleCode' && row.id) {
                    limsSample.loadSampleDetailTopPage(row, true)
                }
            },
        }
    },

    refreshTabInfo: function(row) {
        var _this = this;
        if(Gikam.isNotEmpty(row)) {
            _this.param.id = row.id;
            Gikam.getAsyncComp('sample-abnormal-confirm-uploader').done(function (comp) {
                comp.setOptions({
                    bizId: row.id,
                });
            });
            Gikam.getAsyncComp('sample-abnormal-confirm-auditrecord-grid').done(function (grid) {
                grid.refresh({
                    url: limsSample.auditRecordUrl + '/queries',
                    requestData: {
                        businessId_SEQ: row.id,
                        businessCode_SEQ: 'T_LIMS_SAMPLE'
                    }
                });
            });
        } else {
            _this.param.id = null;
            Gikam.cleanCompData(['sample-abnormal-confirm-auditrecord-grid']);
            Gikam.getAsyncComp('sample-abnormal-confirm-uploader').done(function (comp) {
                comp.setOptions({
                    bizId: -1,
                });
            });
        }
    },

    getAcceptGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-sample-abnormal-accept-list-grid',
            requestData: {
                status_EQ : 'abnormalConfirm',
                abnormalHandleType_EQ : 'accept',
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            columns: limsSample.getSampleAbnormalListColumns(),
            page: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.refreshTabInfo();
                }
            },
            onRowActive: function (index, row) {
                _this.refreshTabInfo(row);
            },
            onCellClick: function (field, row) {
                if (field === 'ext$.batchcode' && row.ext$.batchid) {
                    Gikam.loadBatchDetailPageV12(row.ext$.batchid, true)
                } else if (field === 'sampleCode' && row.id) {
                    limsSample.loadSampleDetailTopPage(row, true)
                }
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var grid = Gikam.getComp('lims-sample-abnormal-accept-list-grid');
                    var selections = grid.getSelections()
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                        let url = limsSample.baseUrl + '/abnormal/confirm'
                        Gikam.postText(url, Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                            grid.refresh()
                        })
                    })
                }
            },{
                type: 'form',
                id: 'warp-form',
                fields: [{
                    field: 'sampleCode_CISC',
                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                }],
            }],
        }
    },

    getSubTabParam: function () {
        var _this = this;
        return {
            type: 'tab',
            fill: true,
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'sample-abnormal-confirm-uploader',
                    dbTable: 'T_LIMS_SAMPLE',
                    formatterGridColumns: function (columns) {
                        columns.splice(2, 0, {
                            field: 'bizCategory',
                            title: 'LIMS.MODLE.FILE.FILETYPE',
                            type: 'select',
                            width: 200,
                            category: 'sys',
                            param: {
                                codeCategoryId: 'sampleFileType'
                            }
                        });
                        return Gikam.getFormatterGridColumns(columns,true);
                    },
                    formatterGridToolbar: function (toolbar) {
                        toolbar.splice(0, 1, {
                            type: 'button',
                            text: 'GIKAM.FILE.BUTTON.UPLOAD_FILE',
                            icon: 'upload',
                            onClick: function () {
                                if (Gikam.isEmpty(_this.param.id)) {
                                    Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                                    return;
                                }
                                Gikam.create('simpleUploader', {
                                    dbTable: 'T_LIMS_SAMPLE',
                                    bizId: _this.param.id,
                                    multiple: false,
                                    autoClose: true,
                                    bizCategory: 'abnormalConfirm',
                                    onAfterClose: function (fileList) {
                                        if (Gikam.isEmpty(fileList)) {
                                            return;
                                        }
                                        Gikam.getComp('sample-abnormal-confirm-uploader').setOptions({
                                            bizId: _this.param.id
                                        })
                                    }
                                });
                            }
                        });
                        return toolbar;
                    },
                    onBeforeDelete: function () {
                        var grid = this.grid;
                        var selections = grid.getSelections();
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                            return;
                        }
                        var bool = selections.filter(s => s.bizCategory !== 'abnormalConfirm');
                        if (bool.length > 0) {
                            Gikam.alert('T_LIMS_SAMPLE.FILE.DELETE_CONFIRM');
                            return false;
                        }
                    }
                }]
            }, {
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [{
                    type: 'grid',
                    id: 'sample-abnormal-confirm-auditrecord-grid',
                    columns: limsSample.getFlowListColumns()
                }]
            }]
        }
    },

    getMainTab: function () {
        var _this = this;
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'SAMPLE.MODLE.TAB.ABNORMAL_STOP',
                items: [_this.getGridParam()]
            }, {
                title: 'SAMPLE.MODLE.TAB.ABNORMAL_ACCEPT',
                items: [_this.getAcceptGridParam()]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不刷新，避免进来就刷新两次表格
                            Gikam.getComp('lims-sample-abnormal-stop-list-grid').refresh()
                        } else {
                            first = true
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp('lims-sample-abnormal-accept-list-grid').done(function (grid) {
                            grid.refresh({
                                url: limsSample.batchSampleUrl + '/select/samples'
                            })
                        })
                        break
                }
            }
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-sample-abnormal-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [_this.getMainTab()]
            },
            center: {
                items: [_this.getSubTabParam()]
            }
        });
    },

    init: function () {
        this.param = {};
        this.create();
    }
};
